// useFormattedDate.js  
 
/** 
 *  @name 组件说明
 *  
 * @param useFormattedDate 自定义处理时间的hoooks 传入一个时间错转换时间格式
 * 
 * @returns formattedDate 返回的是一个 YYYY-MM-DD hh:mm:ss  时间格式
 * 
 * @param usegetTimestampAfterDays 传入天数返回的是传入天数向后计算的时间戳
 * 
 * @returns GetTimestamp  返回的是YYYY-MM-DD格式
 * 
 * 
 * */
export function useFormattedDate(timestamp: any,data?:string) { 

  if(timestamp == '')  return '入店时间错误'
  const date = new Date(timestamp);    
  const year = date.getFullYear();    
  const month = String(date.getMonth() + 1).padStart(2, '0');    
  const day = String(date.getDate()).padStart(2, '0');    
  const hours = String(date.getHours()).padStart(2, '0');    
  const minutes = String(date.getMinutes()).padStart(2, '0');    
  const seconds = String(date.getSeconds()).padStart(2, '0');    
   var formattedTimestamp
  if(data){
    formattedTimestamp = `${year}-${month}-${day}`; 
  }else{
     formattedTimestamp = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;  
  }
  return formattedTimestamp; // 返回格式化后的日期字符串    
}  

export function usegetTimestampAfterDays(days: number) {   
  let today = new Date(); // 获取当前日期时间    
  let timestamp = today.getTime(); // 获取今天的时间戳（以毫秒为单位）    
  let futureTimestamp = timestamp + (days * 24 * 60 * 60 * 1000); // 计算几天后的时间戳  
  const GetTimestamp = useFormattedDate(futureTimestamp,'1')  
  return GetTimestamp;    
}

export function updateTime() {  
  var now = new Date();  
  var hour = now.getHours();  
  var period;  

  if (hour >= 5 && hour < 9) {  
    period = "早上";  
  } else if (hour >= 9 && hour < 12) {  
    period = "上午";  
  } else if (hour >= 12 && hour < 18) {  
    period = "下午";  
  } else if (hour >= 18 && hour < 22) {  
    period = "晚上";  
  } else if (hour >= 22 || hour < 0) {  
    period = "半夜";  
  } else {  
    period = "凌晨";  
  }  

  var timeString = now.toLocaleTimeString();  
  var finalString =  period + " " + timeString;  

  setTimeout(updateTime, 1000); // 每秒更新一次时间  

  return finalString; // 将最终的字符串添加到返回值中  
}